using Admin.Application.Dto;
using Admin.Application.Dto.Role;
using Admin.Application.Repositories;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;

namespace Admin.Api.Controllers;

[Authorize]
[ApiController]
[Route("api/[controller]")]
public class RolesController(IRoleService roleService) : ControllerBase
{
    private readonly IRoleService _roleService = roleService;
    [HttpGet("list")]
    public async Task<IActionResult> GetAll(int pageIndex = 0, int pageSize = 10)
    {
        var result = await _roleService.GetPagedRoleAsync(pageIndex, pageSize);
        return Ok(result);
    }

    [HttpGet("get/{id}")]
    public async Task<IActionResult> GetById(Guid id)
    {
        var result = await _roleService.GetRoleByIdAsync(id);
        return Ok(result);
    }

    [HttpPost("add")]
    public async Task<IActionResult> Add(RoleDto roleDto)
    {
        var result = await _roleService.AddRoleAsync(roleDto);
        return Ok(result);
    }

    [HttpDelete("delete/{id}")]
    public async Task<IActionResult> Delete(Guid id)
    {
        var result = await _roleService.DeleteRoleAsync(id);
        return Ok(result);
    }

    [HttpPut("update/{id}")]
    public async Task<IActionResult> Update(Guid id, RoleDto roleDto)
    {
        var result = await _roleService.UpdateRoleAsync(id, roleDto);
        return Ok(result);
    }

    [HttpPut("update/assign/{roleId}/{permissionId}")]
    public async Task<IActionResult> AssignRolesToUser(Guid roleId, Guid permissionId)
    {
        var result = await _roleService.AssignPermissionsToRoleAsync(roleId, permissionId);
        return Ok(result);
    }

    [HttpPut("update/remove/{roleId}/{permissionId}")]
    public async Task<IActionResult> RemoveRolesToUser(Guid roleId, Guid permissionId)
    {
        var result = await _roleService.RemovePermissionsFromRoleAsync(roleId, permissionId);
        return Ok(result);
    }

    [HttpPut("update/isactive/{id}")]
    public async Task<IActionResult> UpdateIsActive(Guid id, ActiveDto activeDto)
    {
        var result = await _roleService.UpdateIsActive(id, activeDto);
        return Ok(result);
    }
}